/**
 * 
 */
package cz.cvut.fit.crhonjar.mi.run.hawera.symboltable;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cz.cvut.fit.crhonjar.mi.run.hawera.tree.TreeVisitor;

/**
 * @author Jarec
 * 
 */
public class HashSymbolTable implements SymbolTable {

	protected transient Logger log = LoggerFactory.getLogger(this.getClass());

	private Map<String, Object> table;
	private SymbolTable parentTable;

	/**
	 * constructor for HashSymbolTable
	 * 
	 * @param symbolTable
	 */
	public HashSymbolTable(SymbolTable symbolTable) {
		this.log.debug("> HashSymbolTable parent: {}", symbolTable == null);

		this.table = new HashMap<String, Object>();
		this.parentTable = symbolTable;

		this.log.debug("< HashSymbolTable");
	}

	@Override
	public void accept(TreeVisitor visitor) {
		visitor.visitSymbolTable(this);

	}

	@Override
	public SymbolTable getParentTable() {
		this.log.debug(">< getParentTable");
		return this.parentTable;
	}

	@Override
	public boolean insertSymbol(String symbolName, Object symbolValue) {
		this.log.debug("> insertSymbol name: {}, value: {}", symbolName,
				symbolValue);
		this.table.put(symbolName, symbolValue);
		this.log.debug("< insertSymbol");
		return true;
	}

	@Override
	public boolean isSymbolPresent(String symbolName, boolean searchParents) {
		this.log.debug("> isSymbolPresent symbolName: {}, searchParents: {}",
				symbolName, searchParents);
		if (this.table.containsKey(symbolName)) {
			this.log.debug("< isSymbolPresent result: {}", true);
			return true;
		}
		if (searchParents) {
			boolean symbolPresent = this.parentTable.isSymbolPresent(
					symbolName, true);
			this.log.debug("< isSymbolPresent result: {}", symbolPresent);
			return symbolPresent;
		}
		this.log.debug("< isSymbolPresent result: {}", false);
		return false;
	}

}
